<?php
/**
 * 科室介绍页面
 * 州弟医院信息系统 - 州弟学安全
 */

// 包含头文件
include 'includes/header.php';

// 包含数据库连接
require_once 'includes/config.php';

// 获取所有科室
$sql = "SELECT * FROM departments ORDER BY name ASC";
$result = $conn->query($sql);
$departments = [];
if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $departments[] = $row;
    }
}

// 获取特定科室详情（如果有id参数）
$department = null;
$doctors = [];
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
    $dept_id = (int)$_GET['id'];
    
    // 获取科室信息
    $dept_sql = "SELECT * FROM departments WHERE id = $dept_id";
    $dept_result = $conn->query($dept_sql);
    if ($dept_result && $dept_result->num_rows > 0) {
        $department = $dept_result->fetch_assoc();
        
        // 获取该科室的医生 - 简化查询，移除JOIN
        $doc_sql = "SELECT * FROM doctors 
                    WHERE department_id = $dept_id 
                    ORDER BY position, name";
        echo "<!-- SQL调试: $doc_sql -->";
        $doc_result = $conn->query($doc_sql);
        if (!$doc_result) {
            echo "<!-- SQL错误: " . $conn->error . " -->";
        } else if ($doc_result->num_rows > 0) {
            while ($doc = $doc_result->fetch_assoc()) {
                $doctors[] = $doc;
            }
            echo "<!-- 找到 " . count($doctors) . " 名医生 -->";
        } else {
            echo "<!-- 未找到医生 -->";
        }
    }
}

// 页面标题
$pageTitle = isset($department) ? $department['name'] : '科室介绍';
?>

<div class="container py-5">
    <div class="row mb-5">
        <div class="col-lg-8 mx-auto text-center">
            <h1 class="display-4 text-primary"><?php echo $pageTitle; ?></h1>
            <p class="lead text-muted">我院拥有完善的科室设置，先进的医疗设备，为患者提供全方位的医疗服务</p>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-3 mb-4">
            <div class="list-group shadow-sm sticky-top" style="top: 20px;">
                <?php foreach ($departments as $index => $dept): ?>
                <a href="departments.php?id=<?php echo $dept['id']; ?>" class="list-group-item list-group-item-action <?php echo (isset($department) && $department['id'] == $dept['id']) ? 'active' : ''; ?>">
                    <?php echo htmlspecialchars($dept['name']); ?>
                </a>
                <?php endforeach; ?>
            </div>
        </div>
        <div class="col-lg-9">
            <?php if (isset($department)): ?>
                <!-- 科室详细信息 -->
                <div class="card mb-4">
                    <div class="card-header bg-primary text-white">
                        <h3 class="mb-0"><?php echo htmlspecialchars($department['name']); ?></h3>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-4 text-center mb-4 mb-md-0">
                                <div class="bg-light p-3 rounded-circle mx-auto" style="width: 150px; height: 150px;">
                                    <i class="fas fa-hospital-alt fa-5x text-primary mt-4"></i>
                                </div>
                            </div>
                            <div class="col-md-8">
                                <h4>科室简介</h4>
                                <p><?php echo nl2br(htmlspecialchars($department['description'])); ?></p>
                                
                                <?php if (!empty($department['specialties'])): ?>
                                <h4 class="mt-4">专业特长</h4>
                                <p><?php echo nl2br(htmlspecialchars($department['specialties'])); ?></p>
                                <?php endif; ?>
                                
                                <div class="mt-4">
                                    <h4>门诊信息</h4>
                                    <div class="row">
                                        <div class="col-md-6">
                                            <p><strong>门诊地点：</strong> <?php echo htmlspecialchars($department['location'] ?? '医院门诊楼'); ?></p>
                                        </div>
                                        <div class="col-md-6">
                                            <p><strong>咨询电话：</strong> <?php echo htmlspecialchars($department['phone'] ?? '010-12345678'); ?></p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- 科室医生 -->
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <h3 class="mb-0">科室医生</h3>
                    </div>
                    <div class="card-body">
                        <?php if (count($doctors) > 0): ?>
                            <div class="row">
                                <?php foreach ($doctors as $doctor): ?>
                                    <div class="col-md-6 mb-4">
                                        <div class="card h-100">
                                            <div class="row g-0">
                                                <div class="col-4">
                                                    <div class="bg-light d-flex justify-content-center align-items-center h-100">
                                                        <i class="fas fa-user-md fa-3x text-secondary"></i>
                                                    </div>
                                                </div>
                                                <div class="col-8">
                                                    <div class="card-body">
                                                        <h5 class="card-title"><?php echo htmlspecialchars($doctor['name']); ?></h5>
                                                        <p class="card-text">
                                                            <span class="badge bg-primary"><?php echo htmlspecialchars($doctor['position']); ?></span>
                                                        </p>
                                                        <p class="card-text small text-muted">
                                                            <?php 
                                                            echo !empty($doctor['title']) 
                                                                ? htmlspecialchars(mb_substr($doctor['title'], 0, 50)) . (mb_strlen($doctor['title']) > 50 ? '...' : '') 
                                                                : '暂无专长信息';
                                                            ?>
                                                        </p>
                                                        <a href="doctors.php?id=<?php echo $doctor['id']; ?>" class="btn btn-sm btn-outline-primary">查看详情</a>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php else: ?>
                            <div class="alert alert-info">暂无医生信息</div>
                        <?php endif; ?>
                    </div>
                </div>
            <?php else: ?>
                <!-- 科室概览 -->
                <div class="card mb-4">
                    <div class="card-header bg-primary text-white">
                        <h3 class="mb-0">科室概览</h3>
                    </div>
                    <div class="card-body">
                        <p class="lead">州弟医院拥有多个专业科室，提供全面的医疗服务。请从左侧选择科室查看详细信息。</p>
                        
                        <?php if (!empty($departments)): ?>
                            <div class="row mt-4">
                                <?php foreach ($departments as $dept): ?>
                                    <div class="col-md-4 mb-4">
                                        <div class="card h-100">
                                            <div class="card-body text-center">
                                                <i class="fas fa-stethoscope fa-3x text-primary mb-3"></i>
                                                <h5 class="card-title"><?php echo htmlspecialchars($dept['name']); ?></h5>
                                                <p class="card-text small">
                                                    <?php 
                                                    echo !empty($dept['description']) 
                                                        ? htmlspecialchars(mb_substr($dept['description'], 0, 50)) . (mb_strlen($dept['description']) > 50 ? '...' : '') 
                                                        : '暂无科室描述';
                                                    ?>
                                                </p>
                                                <a href="departments.php?id=<?php echo $dept['id']; ?>" class="btn btn-sm btn-primary">查看详情</a>
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php else: ?>
                            <div class="alert alert-info">暂无科室信息</div>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<script>
// 激活滚动监听
$(document).ready(function() {
    // 平滑滚动
    $('.list-group-item').on('click', function(e) {
        if ($(this).attr('href').indexOf('#') !== -1) {
            e.preventDefault();
            var target = $(this).attr('href');
            $('html, body').animate({
                scrollTop: $(target).offset().top - 20
            }, 500);
        }
    });
    
    // 滚动时更新活动项
    $(window).scroll(function() {
        var scrollPosition = $(window).scrollTop();
        
        $('.card').each(function() {
            var currentId = $(this).attr('id');
            if (currentId) {
                var currentOffset = $(this).offset().top;
                
                if (scrollPosition >= currentOffset - 100) {
                    $('.list-group-item').removeClass('active');
                    $('.list-group-item[href="departments.php?id=' + currentId + '"]').addClass('active');
                }
            }
        });
    });
});
</script>

<?php
// 包含尾文件
include 'includes/footer.php';
?> 